<template>
  <div>
    <el-row>
      <el-col :span="6">
        <el-button
          type="primary"
          plain
          @click="newColumn"
          size="small"
          class="btnNew"
          v-no-more-click
          >新建栏目</el-button
        >
        <div class="left">
          <el-tree
            :data="data"
            :props="defaultProps"
            @node-click="handleNodeClick"
          ></el-tree>
        </div>
      </el-col>
      <el-col :span="14" v-if="showRight"
        ><div class="col_wrap">
          <div class="title_btn" v-if="!isNewColumn">
            <span>快速操作</span>
            <el-popconfirm title="确定删除此数权限？" @onConfirm="delColumn">
              <el-button
                v-no-more-click
                slot="reference"
                type="primary"
                size="small"
                >删除此栏目</el-button
              >
            </el-popconfirm>
          </div>
          <el-form
            class="col_from"
            label-width="80px"
            ref="form"
            :model="form"
            :rules="rules"
            status-icon
          >
            <el-row>
              <el-col :span="24">
                <el-form-item label="上级栏目" prop="parentId">
                  <treeselect
                    class="treeSelect"
                    :options="columnData"
                    :clearable="false"
                    v-model="form.parentId"
                    :normalizer="normalizer"
                    :placeholder="placeholder"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item
                  label="栏目名称"
                  prop="columnName"
                  style="float:left"
                >
                  <el-input v-model="form.columnName"></el-input>
                </el-form-item>
                <el-checkbox v-model="displayStatus">不显示栏目</el-checkbox>
              </el-col>
              <el-col :span="24">
                <el-form-item label="排序值" prop="sortStatus">
                  <el-input-number v-model="form.sortStatus"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="24" v-if="!isNewColumn">
                <el-form-item label="栏目id">
                  {{ form.id }}
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="是否房源">
                  <el-checkbox v-model="isHouse"></el-checkbox>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item class="remarks" prop="remarks" label="栏目备注">
                  <el-input
                    type="textarea"
                    placeholder="请输入内容"
                    v-model="form.remarks"
                    :autosize="{ minRows: 6 }"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer clearfix">
            <el-button @click="closeDia" v-no-more-click>取 消</el-button>
            <el-button type="primary" @click="submit" v-no-more-click
              >提交</el-button
            >
          </div>
        </div></el-col
      >
    </el-row>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {
  getColumnList,
  postColSave,
  getColumnOne,
  updateColSave,
  delColumnOne
} from "../../../src/assets/js/api";
export default {
  name: "EditColumn",
  components: {
    Treeselect
  },
  data() {
    return {
      normalizer(node) {
        //去掉children=[]的children属性
        if (node.children == null) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.label,
          children: node.children
        };
      },
      isHouse: false,
      form: {
        parentId: null,
        remarks: null,
        sortStatus: 1,
        displayStatus: 1,
        id: null,
        columnName: null
      },
      showRight: false,
      isNewColumn: false,
      displayStatus: false,
      placeholder: "请选择栏目",
      data: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      columnData: [],
      rules: {
        parentId: [
          { required: true, message: "请选择上级栏目", trigger: "change" }
        ],
        columnName: [
          { required: true, message: "请输入栏目名称", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getColumnList();
  },
  methods: {
    handleNodeClick(data) {
      this.isNewColumn = false;
      this.showRight = true;
      this.getColumnOne(data.id);
    },
    //获取栏目
    getColumnList() {
      getColumnList().then(res => {
        this.data = res.data;
        let arr = [
          { pid: null, id: 0, children: null, label: "此作为一级栏目" }
        ];
        this.columnData = arr.concat(res.data);
      });
    },
    //查询一条栏目信息
    getColumnOne(id) {
      getColumnOne(id).then(res => {
        this.form = res.data;
        this.displayStatus = this.form.displayStatus == 1 ? false : true;
        this.isHouse = this.form.isHouse == 1 ? true : false;
      });
    },
    //新建栏目按钮
    newColumn() {
      this.showRight = true;
      this.isNewColumn = true;
      this.$refs.form.resetFields();
      this.displayStatus = false;
    },
    //删除此栏目
    delColumn() {
      delColumnOne(this.form.id).then(res => {
        this.$message.success(res.message);
        this.getColumnList();
      });
    },
    //取消按钮
    closeDia() {
      this.$emit("dialogCol", false);
    },
    //提交按钮
    submit() {
      if (this.isNewColumn) {
        this.$refs.form.validate().then(() => {
          this.form.id = null;
          this.form.displayStatus = this.displayStatus ? 0 : 1;
          this.form.isHouse = this.isHouse ? 1 : 0;
          postColSave(this.form).then(res => {
            this.$message.success(res.message);
            this.getColumnList();
          });
        });
      } else {
        this.$refs.form.validate().then(() => {
          this.form.displayStatus = this.displayStatus ? 0 : 1;
          this.form.isHouse = this.isHouse ? 1 : 0;
          updateColSave(this.form).then(res => {
            this.$message.success(res.message);
            this.getColumnList();
          });
        });
      }
    }
  }
};
</script>

<style scoped type="less">
.col_wrap {
  text-align: left;
}
.col_from .el-form-item {
  width: 350px;
  margin-bottom: 16px;
}
.col_from .el-form-item.remarks {
  width: 100%;
}
.dialog-footer {
  text-align: center;
}
.el-checkbox {
  line-height: 36px;
  margin-left: 10px;
}
.title_btn {
  height: 40px;
  line-height: 40px;
  margin-bottom: 10px;
}
.title_btn span {
  display: inline-block;
  width: 70px;
  text-align: right;
  padding-right: 10px;
}
.btnNew {
  position: absolute;
  top: -36px;
  left: 0;
}
.left {
  width: 200px;
  height: 390px;
  border: 1px black solid;
  overflow: auto;
}
</style>
